package com.edu.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ShoppingCart implements Serializable {
    public List<ShoppingCartItem> items = new ArrayList<>();

    public List<ShoppingCartItem> getItems() {
        return items;
    }

    public void setItems(List<ShoppingCartItem> items) {
        this.items = items;
    }

    public void addItem(Product product,long quantity){
        for (int i=0;i<items.size();i++){
            if (items.get(i).getProduct().getEpId().equals(product.getEpId())){
                long oldQuantity=items.get(i).getQuantity();
                if (oldQuantity>=items.get(i).getProduct().getEpStock()) return;
                long newQuantity=oldQuantity+quantity;
                items.get(i).setQuantity(newQuantity);
                return;
            }
        }
        items.add(new ShoppingCartItem(product,quantity));
    }

    //修改数量
    public void modifyQuantity(int index, long quantity) {
        items.get(index).setQuantity(quantity);
    }

    //移除一项
    private void removeItem(int index) {
        items.remove(index);
    }

    public Float getTotalCost() {
        float sum = 0;
        for (ShoppingCartItem item : items){
            sum = sum + item.getCost();
        }
        return sum;
    }
}
